package com.test.service.impl;

import com.test.dao.IBuildMeterCurrentDao;
import com.test.dao.IHeatBaseDao;
import com.test.dao.IHouseDao;
import com.test.dao.IHouseMeterCurrentDao;
import com.test.model.*;
import com.test.service.IBuildMeterCurrentService;
import com.test.service.IBuildMeterHistoricalService;
import com.test.service.IBuildValveHistoricalService;
import com.test.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

/**
 * Created by ZZF on 2017/7/3.
 */
@Service
public class BuildMeterCurrentServiceImpl implements IBuildMeterCurrentService {

    @Autowired
    IBuildMeterCurrentDao buildMeterCurrentDao;
    @Autowired
    IHouseDao houseDao;

    @Autowired
    IHeatBaseDao heatBaseDao;

    @Autowired
    IBuildMeterHistoricalService buildMeterHistoricalService;


    @Override
    public List<Map<String, Object>> findStationBranch() {

        return buildMeterCurrentDao.findStationBranch();
    }


    @Override
    public void updateBuildMeter(List<House> houseList, BuildMeterCurrentData buildMeterCurrentData) {


        //准备好要给累加的楼表数据
        BigDecimal bulidFlow = new BigDecimal("0");
        BigDecimal bulidAccFlow = new BigDecimal("0");
        BigDecimal bulidHeat = new BigDecimal("0");
        BigDecimal bulidAccHeat = new BigDecimal("0");

        List supplyTemperatureList = new ArrayList();

        int count_ReturnTemperature = 0;
        int numb = 0;
        int Average_ReturnTemperature = 0;

        boolean isNull = true;

        if (!houseList.isEmpty()) {

            for (House house : houseList) {


                //通过houseid得到户表实时数据对象
                HouseMeterCurrentData houseMeterCurrentData = houseDao.findMeterCurrent(house.getUniqueId());

                if (houseMeterCurrentData != null) {

                    //累加户表的数据就是楼表的数据
                    bulidFlow = bulidFlow.add(BaseUtils.initBigDecimal(houseMeterCurrentData.getFlow()));
                    bulidAccFlow = bulidAccFlow.add(BaseUtils.initBigDecimal(houseMeterCurrentData.getAccFlow()));
                    bulidHeat = bulidHeat.add(BaseUtils.initBigDecimal(houseMeterCurrentData.getHeat()));
                    bulidAccHeat = bulidAccHeat.add(BaseUtils.initBigDecimal(houseMeterCurrentData.getAccHeat()));

                    supplyTemperatureList.add(houseMeterCurrentData.getSupplyTemperature().intValue());

                    count_ReturnTemperature += houseMeterCurrentData.getReturnTemperature().intValue();

                    numb++;
                } else {
                    isNull = false;

                    break;
                }

            }
        } else {
            isNull = false;
        }

        if (isNull) {
            //得到最大的供水温度
            int max_SupplyTemperature = (int) Collections.max(supplyTemperatureList);
            //加上偏差值
            max_SupplyTemperature = max_SupplyTemperature + BaseUtils.deviation();
            //得到最大的回水温度
            Average_ReturnTemperature = count_ReturnTemperature / numb;


            //更新楼表实时数据
            buildMeterCurrentData.setAccFlow(bulidAccFlow);
            buildMeterCurrentData.setFlow(bulidFlow);
            buildMeterCurrentData.setAccHeat(bulidAccHeat);
            buildMeterCurrentData.setHeat(bulidHeat);
            buildMeterCurrentData.setReturnTemperature(BaseUtils.getBigDecimal(Average_ReturnTemperature));
            buildMeterCurrentData.setSupplyTemperature(BaseUtils.getBigDecimal(max_SupplyTemperature));

            Date now = new Date();
            buildMeterCurrentData.setPlanTime(now);

            HeatBase buildBase = heatBaseDao.findBuildBase(buildMeterCurrentData.getBuildMeterId());

            //底数数据
            BigDecimal oldFlow = buildBase.getBaseFlow();
            BigDecimal oldHeat = buildBase.getBaseHeat();

            //结束的数据赋值
            buildBase.setEndDate(now);
            buildBase.setEndHeat(bulidAccHeat);
            buildBase.setEndFlow(bulidAccFlow);

            //季使用量
            BigDecimal baseFlow = BaseUtils.initBigDecimal(bulidAccFlow).subtract(BaseUtils.initBigDecimal(oldFlow));
            BigDecimal baseHeat = BaseUtils.initBigDecimal(bulidAccHeat).subtract(BaseUtils.initBigDecimal(oldHeat));

            //底数对象添加季使用量
            buildBase.setSeasonFlow(baseFlow);
            buildBase.setSeasonHeat(baseHeat);

            heatBaseDao.update(buildBase);

            buildMeterCurrentData.setSeasonHeat(baseHeat);
            buildMeterCurrentData.setSeasonFlow(baseFlow);


            //修改楼表实时数据
            buildMeterCurrentDao.update(buildMeterCurrentData);


            //添加楼表的历史记录
            buildMeterHistoricalService.addHistorical(buildMeterCurrentData);
        }

    }


}
